<html>
<head><meta charset="utf-8"><title>using `macro_rules` macros · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/using.20.60macro_rules.60.20macros.html">using `macro_rules` macros</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="181994951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/using%20%60macro_rules%60%20macros/near/181994951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/using.20.60macro_rules.60.20macros.html#181994951">(Nov 27 2019 at 07:02)</a>:</h4>
<p>My understanding is that <code>macro_rules</code> macros can't be imported with <code>use</code>, because there are some unresolved questions about macro-expansion ordering.  However, I've now discovered that one <em>can</em> in fact use this macro, via reexport and new-style lexically scoped <code>use</code>. Is this a feature, or a bug?</p>
<div class="codehilite"><pre><span></span><span class="k">mod</span> <span class="nn">macros</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="n">_my_macro</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">()</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">_my_macro</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">my_macro</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">mod</span> <span class="nn">b</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">crate</span>::<span class="n">macros</span>::<span class="n">my_macro</span><span class="o">!</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="k">super</span>::<span class="n">macros</span>::<span class="n">my_macro</span><span class="o">!</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="c1">// my_macro!();</span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>cc <span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span></p>



<a name="182051303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/using%20%60macro_rules%60%20macros/near/182051303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/using.20.60macro_rules.60.20macros.html#182051303">(Nov 27 2019 at 19:30)</a>:</h4>
<p>This is a direct consequence of "uniform paths", if the macro can be called, then it can be used as well.</p>



<a name="182051359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/using%20%60macro_rules%60%20macros/near/182051359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/using.20.60macro_rules.60.20macros.html#182051359">(Nov 27 2019 at 19:31)</a>:</h4>
<p>Not sure what are the unresolved questions here.</p>



<a name="182053421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/using%20%60macro_rules%60%20macros/near/182053421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/using.20.60macro_rules.60.20macros.html#182053421">(Nov 27 2019 at 19:55)</a>:</h4>
<p>So, <code>use</code>ing <code>macro_rules</code>  macros (without this hack) is not allowed simply because it's not implemented, and not because there's some big reason not to allow it?</p>



<a name="182053903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/using%20%60macro_rules%60%20macros/near/182053903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/using.20.60macro_rules.60.20macros.html#182053903">(Nov 27 2019 at 20:01)</a>:</h4>
<p>They can't be used because they have strange scoping, not because there's a special rule for <code>use</code> and macros.</p>
<div class="codehilite"><pre><span></span><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}}</span><span class="w"></span>

<span class="k">mod</span> <span class="nn">x</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">use</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="c1">// OK on 2018 - This isn&#39;t allowed on 2015 because `::m!()` isn&#39;t either.</span>
<span class="w">    </span><span class="n">k</span><span class="o">!</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>